home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / OutOfPhase1.01Source / OutOfPhase Folder / WaveTableStorageDisplay.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-01  |  9.0 KB  |  264 lines  |  [TEXT/KAHL]

  1. /* WaveTableStorageDisplay.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
  5. /*    Copyright (C) 1994  Thomas R. Lawrence                                 */
  6. /*                                                                           */
  7. /*    This program is free software; you can redistribute it and/or modify   */
  8. /*    it under the terms of the GNU General Public License as published by   */
  9. /*    the Free Software Foundation; either version 2 of the License, or      */
  10. /*    (at your option) any later version.                                    */
  11. /*                                                                           */
  12. /*    This program is distributed in the hope that it will be useful,        */
  13. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  14. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
  15. /*    GNU General Public License for more details.                           */
  16. /*                                                                           */
  17. /*    You should have received a copy of the GNU General Public License      */
  18. /*    along with this program; if not, write to the Free Software            */
  19. /*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
  20. /*                                                                           */
  21. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  22. /*                                                                           */
  23. /*****************************************************************************/
  24.  
  25. #include "MiscInfo.h"
  26. #include "Audit.h"
  27. #include "Debug.h"
  28. #include "Definitions.h"
  29.  
  30. #include "WaveTableStorageDisplay.h"
  31. #include "Array.h"
  32. #include "Memory.h"
  33. #include "DataMunging.h"
  34.  
  35.  
  36. struct WaveTableStorDispRec
  37.     {
  38.         NumBitsType            NumBits;
  39.         long                        FramesPerTable;
  40.         ArrayRec*                TableArray; /* of largefixedsigned[]'s */
  41.     };
  42.  
  43.  
  44. /* create a new, empty wave table storage display object */
  45. WaveTableStorDispRec*        NewWaveTableStorDisp(NumBitsType NumBits, long NumFrames)
  46.     {
  47.         WaveTableStorDispRec*    StorDisp;
  48.  
  49.         ERROR((NumBits != eSample8bit) && (NumBits != eSample16bit),PRERR(ForceAbort,
  50.             "NewWaveTableStorDisp:  bad number of bits"));
  51.         ERROR((NumFrames != 2) && (NumFrames != 4) && (NumFrames != 8) && (NumFrames != 16)
  52.             && (NumFrames != 32) && (NumFrames != 64) && (NumFrames != 128)
  53.             && (NumFrames != 256) && (NumFrames != 512) && (NumFrames != 1024)
  54.             && (NumFrames != 2048) && (NumFrames != 4096) && (NumFrames != 8192)
  55.             && (NumFrames != 16384) && (NumFrames != 32768) && (NumFrames != 65536),
  56.             PRERR(ForceAbort,"NewWaveTableStorDisp:  bad number of frames"));
  57.         StorDisp = (WaveTableStorDispRec*)AllocPtrCanFail(sizeof(WaveTableStorDispRec),
  58.             "WaveTableStorDispRec");
  59.         if (StorDisp == NIL)
  60.             {
  61.              FailurePoint1:
  62.                 return NIL;
  63.             }
  64.         StorDisp->NumBits = NumBits;
  65.         StorDisp->FramesPerTable = NumFrames;
  66.         StorDisp->TableArray = NewArray();
  67.         if (StorDisp->TableArray == NIL)
  68.             {
  69.              FailurePoint2:
  70.                 ReleasePtr((char*)StorDisp);
  71.                 goto FailurePoint1;
  72.             }
  73.         return StorDisp;
  74.     }
  75.  
  76.  
  77. /* dispose of the wave table storage object */
  78. void                                        DisposeWaveTableStorDisp(WaveTableStorDispRec* StorDisp)
  79.     {
  80.         long                                    Limit;
  81.         long                                    Scan;
  82.  
  83.         CheckPtrExistence(StorDisp);
  84.         Limit = ArrayGetLength(StorDisp->TableArray);
  85.         for (Scan = 0; Scan < Limit; Scan += 1)
  86.             {
  87.                 ReleasePtr((char*)ArrayGetElement(StorDisp->TableArray,Scan));
  88.             }
  89.         DisposeArray(StorDisp->TableArray);
  90.         ReleasePtr((char*)StorDisp);
  91.     }
  92.  
  93.  
  94. /* get the number of frames per table */
  95. long                                        WaveTableStorDispNumFramesPerTable(WaveTableStorDispRec* StorDisp)
  96.     {
  97.         CheckPtrExistence(StorDisp);
  98.         return StorDisp->FramesPerTable;
  99.     }
  100.  
  101.  
  102. /* get the number of tables */
  103. long                                        WaveTableStorDispNumTables(WaveTableStorDispRec* StorDisp)
  104.     {
  105.         CheckPtrExistence(StorDisp);
  106.         return ArrayGetLength(StorDisp->TableArray);
  107.     }
  108.  
  109.  
  110. /* get the number of bits for the wave table */
  111. NumBitsType                            WaveTableStorDispNumBits(WaveTableStorDispRec* StorDisp)
  112.     {
  113.         CheckPtrExistence(StorDisp);
  114.         return StorDisp->NumBits;
  115.     }
  116.  
  117.  
  118. /* change the number of bits for the wave table */
  119. void                                        SetWaveTableStorDispNumBits(WaveTableStorDispRec* StorDisp,
  120.                                                     NumBitsType NewNumBits)
  121.     {
  122.         CheckPtrExistence(StorDisp);
  123.         ERROR((NewNumBits != eSample8bit) && (NewNumBits != eSample16bit),PRERR(ForceAbort,
  124.             "SetWaveTableStorDispNumBits:  bad number of bits"));
  125.         StorDisp->NumBits = NewNumBits;
  126.     }
  127.  
  128.  
  129. /* get a reference to a table.  this is NOT copied, and there is no type information. */
  130. /* it is formatted as an array of characters for 8 bit data or array of shorts for */
  131. /* 16 bit data.  it contains (WaveTableStorDispNumFramesPerTable + 1) elements for */
  132. /* each table.  The last element is a repeat of the first, and is provided for */
  133. /* making anti-aliasing more efficient. */
  134. largefixedsigned*                WaveTableStorDispGetTable(WaveTableStorDispRec* StorDisp,
  135.                                                     long Index)
  136.     {
  137.         CheckPtrExistence(StorDisp);
  138.         ERROR((Index < 0) || (Index >= WaveTableStorDispNumTables(StorDisp)),
  139.             PRERR(ForceAbort,"WaveTableStorDispGetTable:  index out of range"));
  140.         return (largefixedsigned*)ArrayGetElement(StorDisp->TableArray,Index);
  141.     }
  142.  
  143.  
  144. /* append a new (zeroed out) table to the end of the array */
  145. MyBoolean                                WaveTableStorDispAppendEntry(WaveTableStorDispRec* StorDisp)
  146.     {
  147.         largefixedsigned*            NewSlice;
  148.         long                                    Scan;
  149.  
  150.         CheckPtrExistence(StorDisp);
  151.         NewSlice = (largefixedsigned*)AllocPtrCanFail(sizeof(largefixedsigned)
  152.             * (StorDisp->FramesPerTable + 1),"WaveTableStorDispRec slice");
  153.         if (NewSlice == NIL)
  154.             {
  155.              FailurePoint1:
  156.                 return False;
  157.             }
  158.         if (!ArrayAppendElement(StorDisp->TableArray,NewSlice))
  159.             {
  160.              FailurePoint2:
  161.                 ReleasePtr((char*)NewSlice);
  162.                 goto FailurePoint1;
  163.             }
  164.         for (Scan = 0; Scan < StorDisp->FramesPerTable + 1; Scan += 1)
  165.             {
  166.                 PRNGCHK(NewSlice,&(NewSlice[Scan]),sizeof(NewSlice[Scan]));
  167.                 NewSlice[Scan] = 0;
  168.             }
  169.         return True;
  170.     }
  171.  
  172.  
  173. /* put a value into a frame in a table */
  174. void                                        WaveTableStorDispSetFrame(WaveTableStorDispRec* StorDisp,
  175.                                                     long TableIndex, long FrameIndex, largefixedsigned Value)
  176.     {
  177.         largefixedsigned*            TheSlice;
  178.  
  179.         CheckPtrExistence(StorDisp);
  180.         ERROR((TableIndex < 0) || (TableIndex >= WaveTableStorDispNumTables(StorDisp)),
  181.             PRERR(ForceAbort,"WaveTableStorDispSetFrame:  table index out of range"));
  182.         ERROR((FrameIndex < 0) || (FrameIndex >= StorDisp->FramesPerTable),
  183.             PRERR(ForceAbort,"WaveTableStorDispSetFrame:  frame in table index out of range"));
  184.         TheSlice = WaveTableStorDispGetTable(StorDisp,TableIndex);
  185.         PRNGCHK(TheSlice,&(TheSlice[FrameIndex]),sizeof(TheSlice[FrameIndex]));
  186.         TheSlice[FrameIndex] = Value;
  187.         if (FrameIndex == 0)
  188.             {
  189.                 PRNGCHK(TheSlice,&(TheSlice[StorDisp->FramesPerTable]),
  190.                     sizeof(TheSlice[StorDisp->FramesPerTable]));
  191.                 TheSlice[StorDisp->FramesPerTable] = Value; /* anti-aliasing loopback value */
  192.             }
  193.     }
  194.  
  195.  
  196. /* get a value from a frame in a table */
  197. largefixedsigned                WaveTableStorDispGetFrame(WaveTableStorDispRec* StorDisp,
  198.                                                     long TableIndex, long FrameIndex)
  199.     {
  200.         largefixedsigned*            TheSlice;
  201.  
  202.         CheckPtrExistence(StorDisp);
  203.         ERROR((TableIndex < 0) || (TableIndex >= WaveTableStorDispNumTables(StorDisp)),
  204.             PRERR(ForceAbort,"WaveTableStorDispGetFrame:  table index out of range"));
  205.         ERROR((FrameIndex < 0) || (FrameIndex >= StorDisp->FramesPerTable),
  206.             PRERR(ForceAbort,"WaveTableStorDispGetFrame:  frame in table index out of range"));
  207.         TheSlice = WaveTableStorDispGetTable(StorDisp,TableIndex);
  208.         PRNGCHK(TheSlice,&(TheSlice[FrameIndex]),sizeof(TheSlice[FrameIndex]));
  209.         return TheSlice[FrameIndex];
  210.     }
  211.  
  212.  
  213. /* make a duplicate of the wave table */
  214. WaveTableStorDispRec*        WaveTableStorDispDuplicate(WaveTableStorDispRec* Original)
  215.     {
  216.         WaveTableStorDispRec*    Copy;
  217.         long                                    Limit;
  218.         long                                    Scan;
  219.  
  220.         CheckPtrExistence(Original);
  221.         Copy = (WaveTableStorDispRec*)AllocPtrCanFail(sizeof(WaveTableStorDispRec),
  222.             "WaveTableStorDispRec");
  223.         if (Copy == NIL)
  224.             {
  225.              FailurePoint1:
  226.                 return NIL;
  227.             }
  228.         Copy->NumBits = Original->NumBits;
  229.         Copy->FramesPerTable = Original->FramesPerTable;
  230.         Copy->TableArray = NewArray();
  231.         if (Copy->TableArray == NIL)
  232.             {
  233.              FailurePoint2:
  234.                 ReleasePtr((char*)Copy);
  235.                 goto FailurePoint1;
  236.             }
  237.         Limit = ArrayGetLength(Original->TableArray);
  238.         for (Scan = 0; Scan < Limit; Scan += 1)
  239.             {
  240.                 char*                                    Temp;
  241.  
  242.                 Temp = CopyPtr((char*)ArrayGetElement(Original->TableArray,Scan));
  243.                 if (Temp == NIL)
  244.                     {
  245.                      FailurePoint3:
  246.                         Limit = ArrayGetLength(Copy->TableArray);
  247.                         for (Scan = 0; Scan < Limit; Scan += 1)
  248.                             {
  249.                                 ReleasePtr((char*)ArrayGetElement(Copy->TableArray,Scan));
  250.                             }
  251.                         DisposeArray(Copy->TableArray);
  252.                         goto FailurePoint2;
  253.                     }
  254.                 SetTag(Temp,"WaveTableStorDispRec:  slice");
  255.                 if (!ArrayAppendElement(Copy->TableArray,Temp))
  256.                     {
  257.                      FailurePoint3a:
  258.                         ReleasePtr((char*)Temp);
  259.                         goto FailurePoint3;
  260.                     }
  261.             }
  262.         return Copy;
  263.     }
  264.